package com.betty.cscams.controller;

import java.util.List;

import com.betty.cscams.common.http.HttpResult;
import com.betty.cscams.common.page.PageRequest;
import com.betty.cscams.constant.SysConstants;
import com.betty.cscams.dao.SysUserMapper;
import com.betty.cscams.entity.SysUser;
import com.betty.cscams.entity.SysUserDept;
import com.betty.cscams.service.SysUserService;
import com.betty.cscams.util.PasswordUtils;
import com.betty.cscams.vo.UserInfoBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;



/**
 * 用户控制器
 */
@RestController
@RequestMapping("user")
public class SysUserController {

	@Autowired
	private SysUserService sysUserService;
	@Autowired
	private SysUserMapper sysUserMapper;
	
	@PostMapping(value="/save")
	public HttpResult save(@RequestBody SysUser record) {
		SysUser user = sysUserService.findById(record.getId());
		if(user != null) {
			if(SysConstants.ADMIN.equalsIgnoreCase(user.getName())) {
				return HttpResult.error("超级管理员不允许修改!");
			}
		}
			String salt = PasswordUtils.getSalt();
			if(user == null) {
				// 新增用户
				if(sysUserService.findByName(record.getName()) != null) {
					return HttpResult.error("用户名已存在!");
				}
				String initPwd = ("student".equals(record.getType()) || "teacher".equals(record.getType())) ? "#123456" : "@123456";
				String password = PasswordUtils.encode(initPwd, salt);
				record.setSalt(salt);
				record.setPassword(password);
			}
		return HttpResult.ok(sysUserService.save(record));
	}

	@PostMapping(value="/delete")
	public HttpResult delete(@RequestBody List<SysUser> records) {
		for(SysUser record:records) {
			SysUser sysUser = sysUserService.findById(record.getId());
			if(sysUser != null && SysConstants.ADMIN.equalsIgnoreCase(sysUser.getName())) {
				return HttpResult.error("超级管理员不允许删除!");
			}
		}
		return HttpResult.ok(sysUserService.delete(records));
	}
	
	@GetMapping(value="/findByName")
	public HttpResult findByUserName(@RequestParam String name) {
		return HttpResult.ok(sysUserService.findByName(name));
	}
	
	@GetMapping(value="/findPermissions")
	public HttpResult findPermissions(@RequestParam String name) {
		return HttpResult.ok(sysUserService.findPermissions(name));
	}
	
	@GetMapping(value="/findUserRoles")
	public HttpResult findUserRoles(@RequestParam Long userId) {
		return HttpResult.ok(sysUserService.findUserRoles(userId));
	}

	@PostMapping(value="/findPage")
	public HttpResult findPage(@RequestBody PageRequest pageRequest) {
		return HttpResult.ok(sysUserService.findPage(pageRequest));
	}



	@PostMapping(value="/saveUserDept")
	public HttpResult saveUserDept(@RequestBody List<SysUserDept> records) {
		for(SysUserDept record:records) {
			SysUser sysUser = sysUserMapper.selectByPrimaryKey(record.getUserId());
			if(SysConstants.ADMIN.equalsIgnoreCase(sysUser.getName())) {
				// 如果是超级管理员，不允许修改
				return HttpResult.error("超级管理员拥有所有班级权限，不允许修改！");
			}
		}
		return HttpResult.ok(sysUserService.saveUserDept(records));
	}


	@GetMapping(value="/findUserOptions")
	public HttpResult findUserOptions() {
		return HttpResult.ok(sysUserService.findUserOptions());
	}

	@GetMapping(value="/findAuditOptions")
	public HttpResult findAuditOptions() {
		return HttpResult.ok(sysUserService.findAuditOptions());
	}


	@GetMapping(value="/updatePassword")
	public HttpResult updatePassword(@RequestParam String password,@RequestParam String newPassword) {
		return HttpResult.ok(sysUserService.updatePassword(password,newPassword));
	}
	@PostMapping(value="/updatePersonInfo")
	public HttpResult updatePersonInfo(@RequestBody UserInfoBean userInfo) {
		return HttpResult.ok(sysUserService.updatePersonInfo(userInfo));
	}

}
